# Makefile
#
# ============================================================================
# Copyright (c) Texas Instruments Inc 2007
#
# Use of this software is controlled by the terms and conditions found in the
# license agreement under which this software has been supplied or provided.
# ============================================================================

TARGET = $(notdir $(CURDIR))

ROOTDIR	= ..

include $(ROOTDIR)/Rules.make
include dvsdk_Rules.make

MCVIP_PATH	= mcvip_dm6446_tvp5158_1118
MCVIP_INCS	= -I$(MCVIP_PATH)/osa/inc -I$(MCVIP_PATH)/mcvip/inc -I$(MCVIP_PATH)/test/mcvip/inc
CMEM_PATH	= $(CMEM_INSTALL_DIR)/packages/ti/sdo/linuxutils/cmem
CMEM_INCS	= -I$(CMEM_PATH)/include
MCVIP_LIBS	= $(MCVIP_PATH)/lib/mcvip_test.a \
						$(MCVIP_PATH)/lib/mcvip.a \
						$(MCVIP_PATH)/lib/osa.a \
						$(MCVIP_PATH)/lib/drv.a
CMEM_LIBS	= $(CMEM_PATH)/lib/cmem.a

# Where to output configuration files
XDC_CFG = $(TARGET)_config

# Output compiler options
XDC_CFLAGS = $(XDC_CFG)/compiler.opt

# Output linker file
XDC_LFILE = $(XDC_CFG)/linker.cmd

# Input configuration file
XDC_CFGFILE = $(TARGET).cfg

# Target tools
XDC_TARGET = gnu.targets.MVArm9

# Platform (board) to build for
XDC_PLATFORM = ti.platforms.evmDM6446

# Package path for the XDC tools
XDC_PATH = /home/cooper/dvsdk_1_30_01_41/codec_engine_2_00_01/examples/packages;$(CODEC_INSTALL_DIR)/packages;$(CE_INSTALL_DIR)/packages;$(LINK_INSTALL_DIR)/packages;$(XDAIS_INSTALL_DIR)/packages;$(CMEM_INSTALL_DIR)/packages;$(FC_INSTALL_DIR)/packages

# The XDC configuration tool command line
CONFIGURO = XDCPATH="$(XDC_PATH)" $(XDC_INSTALL_DIR)/xs xdc.tools.configuro

C_FLAGS += -Wall -Dti_sdo_ce_osal_Memory_USEDEPRECATEDAPIS=1 -I$(ROOTDIR)/include $(MCVIP_INCS) $(CMEM_INCS)

CPP_FLAGS += -I$(DVEVM_INSTALL_DIR)/demos/dm6446/utils/include \
		-I$(LINUXKERNEL_INSTALL_DIR)/include -I$(ROOTDIR)/include

LD_FLAGS += -lfreetype -lpng -ljpeg -lpthread -lm

COMPILE.c = $(MVTOOL_PREFIX)gcc $(C_FLAGS) $(CPP_FLAGS) -c
LINK.c = $(MVTOOL_PREFIX)gcc $(LD_FLAGS)

DBGTARGET = debug/$(TARGET)d
RELTARGET = release/$(TARGET)

DBGCFLAGS = -g3 -D__DEBUG
RELCFLAGS = -O2 -fno-strict-aliasing

SOURCES = $(wildcard *.c)
HEADERS = $(wildcard *.h)

DBGOBJFILES = $(SOURCES:%.c=debug/%.o)
RELOBJFILES = $(SOURCES:%.c=release/%.o)

RELLDFLAGS = $(DVEVM_INSTALL_DIR)/demos/dm6446/utils/lib/simplewidget.a \
		$(DVEVM_INSTALL_DIR)/demos/dm6446/utils/lib/msp430lib.a \
		$(ROOTDIR)/lib/libmegaEyes.a \
		
DBGLDFLAGS = $(DVEVM_INSTALL_DIR)/demos/dm6446/utils/lib/simplewidgetd.a \
		$(DVEVM_INSTALL_DIR)/demos/dm6446/utils/lib/msp430libd.a \
		$(ROOTDIR)/lib/libmegaEyes.a \

.PHONY: clean debug release install

all:	debug release

install:
	install -d $(EXEC_DIR)
	install $(RELTARGET) $(EXEC_DIR)
	install -m 666 $(CODEC_INSTALL_DIR)/ti/sdo/servers/encode/encodeCombo.x64P $(EXEC_DIR)

release:	$(RELTARGET)

debug:		$(DBGTARGET)

$(RELTARGET):	$(RELOBJFILES) $(XDC_LFILE) $(MCVIP_LIBS) $(CMEM_LIBS)
	$(LINK.c) -o $@ $^ $(RELLDFLAGS)
	cp $@ ../bin

$(DBGTARGET):	$(DBGOBJFILES) $(XDC_LFILE) $(MCVIP_LIBS) $(CMEM_LIBS)
	$(LINK.c) -o $@ $^ $(DBGLDFLAGS)

$(RELOBJFILES):	release/%.o: %.c $(HEADERS) $(XDC_CFLAGS)
	@mkdir -p release
	$(COMPILE.c) $(RELCFLAGS) $(shell cat $(XDC_CFLAGS)) -o $@ $<

$(DBGOBJFILES):	debug/%.o: %.c $(HEADERS) $(XDC_CFLAGS)
	@mkdir -p debug
	$(COMPILE.c) $(DBGCFLAGS) $(shell cat $(XDC_CFLAGS)) -o $@ $<

$(XDC_LFILE) $(XDC_CFLAGS):	$(XDC_CFG)

$(XDC_CFG):	$(XDC_CFGFILE)
	$(CONFIGURO) -c $(MVTOOL_DIR) -o $(XDC_CFG) -t $(XDC_TARGET) \
		-p $(XDC_PLATFORM) $(XDC_CFGFILE)

$(MCVIP_LIBS):
	make -C $(MCVIP_PATH)/build libs

clean:
	-$(RM) -rf $(XDC_CFG) release debug *~ *.d .dep
	make -C $(MCVIP_PATH)/build clean
	
